/**
 *
 */
package net.haroeris.wh40kcampaign.beans.po;

import java.util.Collection;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * @author user
 *
 */
@Entity
@Table(name=Player.TABLE_NAME)
@NamedQuery(name=Player.QUERY_FINDALL, query="SELECT p FROM Player p")
public class Player {
	public static final String QUERY_FINDALL= "Player.findAll";
	public static final String TABLE_NAME= "player";

	@Id
	private String username;

	/** The User names password*/
	private String password;

	/** Is the player enabled (able to log in) */
	private boolean enabled;

	/** The player has to change the password with the next login */
	//    private boolean changePasswd;

	@ElementCollection(fetch=FetchType.EAGER)
	@CollectionTable(name="player_auth", joinColumns=@JoinColumn(name="username"))
	@Column(name="authority")
	/** List of roles a user has. */
	private Collection<String> authorities;

	@OneToMany(mappedBy="player", targetEntity=Army.class)
	private Collection<Army> armies;

	/**
	 * @return the name
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param name the name to set
	 */
	public void setUsername(String name) {
		username = name;
	}

	/**
	 * @return the armies
	 */
	public Collection<Army> getArmies() {
		return armies;
	}

	/**
	 * @param armies the armies to set
	 */
	public void setArmies(Collection<Army> armies) {
		this.armies = armies;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the enabled
	 */
	public boolean isEnabled() {
		return enabled;
	}

	/**
	 * @param enabled the enabled to set
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	@Override
	public String toString(){
		StringBuffer buffer= new StringBuffer();
		buffer.append("[").append(getClass().getSimpleName()).append(" username=").append(getUsername());

		if( getAuthorities()!=null ){
			buffer.append("; authorities=");
			boolean first= true;
			for( String authority : getAuthorities() ){
				if( first ){
					buffer.append("(");
				} else {
					buffer.append(", ");
				}
				buffer.append( authority );
			}
			buffer.append(")");
		}
		buffer.append("]");

		return buffer.toString();
	}

	/**
	 * @return the authorities
	 */
	public Collection<String> getAuthorities() {
		return authorities;
	}

	/**
	 * @param authorities the authorities to set
	 */
	public void setAuthorities(Collection<String> authorities) {
		this.authorities = authorities;
	}
}